
#encoding=UTF-8

from Error import *
from Object import *

class Building(Object):
	def __init__(self):
		Object.__init__(self)
		self.number_available_dsec = ""
		self.level_info_desc = ""
		self.level_info = {}

	def GetMaxNumber(self, townhall_level):
		ps = self.number_available_dsec.split()
		c = len(ps)
		if townhall_level == 0:
			return 0
		if townhall_level<1 or townhall_level>c:
			raise InternalError
		return int(ps[townhall_level-1])

	def GetMaxLevel(self, townhall_level):
		idx = self._TownHallLevelRequiredIndex()
		max = 0
		for k,v in self.level_info.items():
			if townhall_level>=int(v[idx]) and k>max:
				max = k
		return max

	def GetBuildCost(self, level):
		idx = self._BuildCostIndex()
		return (self._BuildCostType(), int(self.level_info[level][idx]))

	def GetBuildTime(self, level):
		idx = self._BuildTimeIndex()
		return eval(self.level_info[level][idx])

	def GetExperienceGained(self, level):
		idx = self._ExperienceGainedIndex()
		return int(self.level_info[level][idx])

	def GetHitPoints(self, level):
		idx = self._HitPointsIndex()
		return int(self.level_info[level][idx])

	def _DoParse(self):
		for ln in self.level_info_desc.splitlines():
			if ln != "":
				ln = ln.replace("Instant", "0")
				ln = ln.replace("N/A", "0")
				ln = ln.replace("minutes", "*60")
				ln = ln.replace("minute", "*60")
				ln = ln.replace("hours", "*3600")
				ln = ln.replace("hour", "*3600")
				ln = ln.replace("days", "*86400")
				ln = ln.replace("day", "*86400")
				ln = ln.replace("m", "*60")
				ln = ln.replace("h", "*3600")
				ln = ln.replace("d", "*86400")
				ln = ln.replace(",", "")
				ln = ln.replace(" ", "")
				#print ln
				ps = ln.split("\t")
				self.level_info[int(ps[0])] = ps

	def _HitPointsIndex(self):
		raise InternalError

	def _BuildCostType(self):
		raise InternalError

	def _BuildCostIndex(self):
		raise InternalError

	def _BuildTimeIndex(self):
		raise InternalError

	def _ExperienceGainedIndex(self):
		raise InternalError

	def _TownHallLevelRequiredIndex(self):
		raise InternalError

